﻿Imports System.Data.SqlClient

Public Class clsDieselTemp
    Private mintIdDiesel As Integer
    Private mintIdTanque As Integer
    Private mintNoCarga As Integer
    Private mdatFecha As Date
    Private mintIdAutobus As Integer
    Private mstrNoEconomico As String
    Private mintIdRuta As Integer
    Private mstrNombreRuta As String
    Private mdblLitros As Double
    Private mdblKilometrosTotales As Double
    Private mdblKilometrosOperacion As Double
    Private mdblKilometrosMuertos As Double
    Private mdblSubtotal As Double
    Private mdblIVA As Double
    Private mdblTotal As Double
    Public Property IdDiesel() As Integer
        Get
            Return mintIdDiesel
        End Get
        Set(ByVal value As Integer)
            mintIdDiesel = value
        End Set
    End Property
    Public Property IdTanque() As Integer
        Get
            Return mintIdTanque
        End Get
        Set(ByVal value As Integer)
            mintIdTanque = value
        End Set
    End Property
    Public Property NoCarga() As Integer
        Get
            Return mintNoCarga
        End Get
        Set(ByVal value As Integer)
            mintNoCarga = value
        End Set
    End Property
    Public Property Fecha As Date
        Get
            Return mdatFecha
        End Get
        Set(ByVal value As Date)
            mdatFecha = value
        End Set
    End Property
       Public Property IdAutobus() As Integer
        Get
            Return mintIdAutobus
        End Get
        Set(ByVal value As Integer)
            mintIdAutobus = value
        End Set
    End Property
    Public Property NoEconomico() As String
        Get
            Return mstrNoEconomico
        End Get
        Set(value As String)
            mstrNoEconomico = value
        End Set
    End Property
    Public Property IdRuta() As Integer
        Get
            Return mintIdRuta
        End Get
        Set(ByVal value As Integer)
            mintIdRuta = value
        End Set
    End Property
    Public Property NombreRuta() As String
        Get
            Return mstrNombreRuta
        End Get
        Set(value As String)
            mstrNombreRuta = value
        End Set
    End Property
    Public Property Litros() As Double
        Get
            Return mdblLitros
        End Get
        Set(ByVal value As Double)
            mdblLitros = value
        End Set
    End Property
    Public Property KilometrosTotales() As Double
        Get
            Return mdblKilometrosTotales
        End Get
        Set(ByVal value As Double)
            mdblKilometrosTotales = value
        End Set
    End Property
    Public Property KilometrosOperacion() As Double
        Get
            Return mdblKilometrosOperacion
        End Get
        Set(ByVal value As Double)
            mdblKilometrosOperacion = value
        End Set
    End Property
    Public Property KilometrosMuertos() As Double
        Get
            Return mdblKilometrosMuertos
        End Get
        Set(ByVal value As Double)
            mdblKilometrosMuertos = value
        End Set
    End Property
    Public Property Subtotal() As Double
        Get
            Return mdblSubtotal
        End Get
        Set(ByVal value As Double)
            mdblSubtotal = value
        End Set
    End Property
    Public Property IVA() As Double
        Get
            Return mdblIVA
        End Get
        Set(ByVal value As Double)
            mdblIVA = value
        End Set
    End Property
    Public Property Total() As Double
        Get
            Return mdblTotal
        End Get
        Set(ByVal value As Double)
            mdblTotal = value
        End Set
    End Property
    Public Sub New()
        mintIdDiesel = 0
        mintIdTanque = 0
        mintNoCarga = 0
        mdatFecha = Now()
        mintIdAutobus = 0
        mstrNoEconomico = ""
        mintIdRuta = 0
        mstrNombreRuta = ""
        mdblLitros = 0
        mdblKilometrosTotales = 0
        mdblKilometrosOperacion = 0
        mdblKilometrosMuertos = 0
        mdblSubtotal = 0
        mdblIVA = 0
        mdblTotal = 0
         End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From DieselTemp " & _
        " WHERE IdDiesel=" & mintIdDiesel

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mintIdDiesel = rdBuscar("IdDiesel")
            mintIdTanque = rdBuscar("IdTanque")
            mintNoCarga = rdBuscar("NoCarga")
            mdatFecha = rdBuscar("Fecha")
            mintIdAutobus = rdBuscar("IdAutobus")
            mstrNoEconomico = rdBuscar("NoEconomico").ToString.Trim
            mintIdRuta = rdBuscar("IdRuta")
            mstrNombreRuta = rdBuscar("NombreRuta").ToString.Trim
            mdblLitros = rdBuscar("Litros")
            mdblKilometrosTotales = rdBuscar("KilometrosTotales")
            mdblKilometrosOperacion = rdBuscar("KilometrosOperacion")
            mdblKilometrosMuertos = rdBuscar("KilometrosMuertos")
            mdblSubtotal = rdBuscar("SubTotal")
            mdblIVA = rdBuscar("IVA")
            mdblTotal = rdBuscar("Total")
        Else

            mintIdDiesel = 0
            mintIdTanque = 0
            mintNoCarga = 0
            mdatFecha = Now()
            mintIdAutobus = 0
            mstrNoEconomico = ""
            mintIdRuta = 0
            mstrNombreRuta = ""
            mdblLitros = 0
            mdblKilometrosTotales = 0
            mdblKilometrosOperacion = 0
            mdblKilometrosMuertos = 0
            mdblSubtotal = 0
            mdblIVA = 0
            mdblTotal = 0
            End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into DieselTemp (" & _
                            "IdTanque," & _
                            "NoCarga," & _
                            "Fecha," & _
                            "IdAutobus," & _
                            "NoEconomico," & _
                            "IdRuta," & _
                            "NombreRuta," & _
                            "Litros," & _
                            "KilometrosTotales," & _
                            "KilometrosOperacion," & _
                            "KilometrosMuertos," & _
                            "SubTotal," & _
                            "IVA," & _
                            "Total)" & _
                            "VALUES (" & _
                            "@IdTanque," & _
                            "@NoCarga," & _
                            "@Fecha," & _
                            "@IdAutobus," & _
                            "substring(@NoEconomico,1,50)," & _
                            "@IdRuta," & _
                            "substring(@NombreRuta,1,100)," & _
                            "@Litros," & _
                            "@KilometrosTotales," & _
                            "@KilometrosOperacion," & _
                            "@KilometrosMuertos," & _
                            "@SubTotal," & _
                            "@IVA," & _
                            "@Total)"
                            
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdTanque", mintIdTanque)
        Command.Parameters.AddWithValue("NoCarga", mintNoCarga)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("NoEconomico", mstrNoEconomico)
        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("NombreRuta", mstrNombreRuta)
        Command.Parameters.AddWithValue("Litros", mdblLitros)
        Command.Parameters.AddWithValue("KilometrosTotales", mdblKilometrosTotales)
        Command.Parameters.AddWithValue("KilometrosOperacion", mdblKilometrosOperacion)
        Command.Parameters.AddWithValue("KilometrosMuertos", mdblKilometrosMuertos)
        Command.Parameters.AddWithValue("Subtotal", mdblSubtotal)
        Command.Parameters.AddWithValue("IVA", mdblIVA)
        Command.Parameters.AddWithValue("Total", mdblTotal)
        
        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar la Liquidacion ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update DieselTemp Set " & _
                    "IdTanque=@IdTanque," & _
                    "NoCarga=@NoCarga," & _
                    "Fecha=@Fecha," & _
                    "IdAutobus=@IdAutobus," & _
                    "NoEconomico=substring(@NoEconomico,1,50)," & _
                    "IdRuta=@IdRuta," & _
                    "NombreRuta=substring(@NombreRuta,1,100)," & _
                    "Litros=@Litros," & _
                    "KilometrosTotales=@KilometrosTotales," & _
                    "KilometrosOperacion=@KilometrosOperacion," & _
                    "KilometrosMuertos=@KilometrosMuertos," & _
                    "Subtotal=@Subtotal," & _
                    "IVA=@IVA," & _
                    "Total=@Total " & _
                    "Where IdDiesel=@IdDiesel"


        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("IdDiesel", mintIdDiesel)
        Command.Parameters.AddWithValue("IdTanque", mintIdTanque)
        Command.Parameters.AddWithValue("NoCarga", mintNoCarga)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("NoEconomico", mstrNoEconomico)
        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("NombreRuta", mstrNombreRuta)
        Command.Parameters.AddWithValue("Litros", mdblLitros)
        Command.Parameters.AddWithValue("KilometrosTotales", mdblKilometrosTotales)
        Command.Parameters.AddWithValue("KilometrosOperacion", mdblKilometrosOperacion)
        Command.Parameters.AddWithValue("KilometrosMuertos", mdblKilometrosMuertos)
        Command.Parameters.AddWithValue("Subtotal", mdblSubtotal)
        Command.Parameters.AddWithValue("IVA", mdblIVA)
        Command.Parameters.AddWithValue("Total", mdblTotal)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el liquidación ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
